package com.tech.repository.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * 订单实体类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Serializable {
	private Integer boid;
	/**
	 * 买家ID
	 */
	private Integer buid;
	/**
	 * 商品ID
	 */
	private Integer gid;
	/**
	 * 地址ID，可修改
	 */
	private Integer adid;
	/**
	 * 订单总价
	 */
	private double bocost;
	/**
	 * 订单状态，对应 OrderState 中的枚举类型
	 */
	private OrderState bostate;
	/**
	 * 配送方式，对应 OrderSendMode 中的枚举类型
	 */
	private Goods.SendMode bosend;
	private Date bcreatetime;

	public Order(Integer boid, Integer buid, Integer sellerId) {
		this(boid, buid, sellerId, null, 0, null, null, null);
	}

	/**
	 * 创建订单，该商品处于已拍下状态
	 *
	 * @param buid   买家ID
	 * @param gid   商品ID
	 * @param adid 地址ID，可修改
	 * @param bocost      订单总价
	 * @param bosend  配送方式，对应 OrderSendMode 中的枚举类型
	 */
	public Order(Integer buid, Integer gid, Integer adid, double bocost, Goods.SendMode bosend) {
		this(null, buid, gid, adid, bocost, OrderState.UNDER, bosend, new Date());
	}

	/**
	 * 所有订单状态的集合
	 */
	public enum OrderState {
		/**
		 * 初始值，未购买
		 */
		INIT,
		/**
		 * 已拍下，但还未付钱
		 */
		UNDER,
		/**
		 * 已付款，商家还没发货
		 */
		PAYMENT,
		/**
		 * 已发货，商家发货了但是商品还没送到或是买家还没去领
		 */
		DELIVER,
		/**
		 * 交易成功，买家已收到商品，但还未评价
		 */
		TRADE,
		/**
		 * 已评价
		 */
		REVIEW;

		public static OrderState parse(String str) {
			OrderState result = null;
			switch (str) {
				case "未购买" -> result = INIT;
				case "已拍下" -> result = UNDER;
				case "已付款" -> result = PAYMENT;
				case "已发货" -> result = DELIVER;
				case "交易成功" -> result = TRADE;
				case "已评价" -> result = REVIEW;
			}
			return result;
		}

		@Override
		public String toString() {
			String result = null;
			switch (this) {
				case INIT -> result = "未购买";
				case UNDER -> result = "已拍下";
				case PAYMENT -> result = "已付款";
				case DELIVER -> result = "已发货";
				case TRADE -> result = "交易成功";
				case REVIEW -> result = "已评价";
			}
			return result;
		}
	}
}
